package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.db.domain.MarketBrandExample;

import com.cskaoyan.market.db.mapper.MarketBrandMapper;
import com.cskaoyan.market.service.AdminBrandService;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminBrandServiceImpl implements AdminBrandService {

    @Override
    public List<MarketBrand> list(String page, String limit, String sort, String order,String id, String name) {
        SqlSession session = MybatisUtils.getSession();
        MarketBrandMapper marketBrandMapper = session.getMapper(MarketBrandMapper.class);
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        if(!StringUtils.isEmpty(id)){
            criteria.andIdEqualTo(Integer.parseInt(id));
        }
        marketBrandExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        session.close();
        return marketBrands;

    }

    @Override
    public int create(MarketBrand marketBrand) {
        SqlSession session = MybatisUtils.getSession();
        MarketBrandMapper marketBrandMapper = session.getMapper(MarketBrandMapper.class);

        int effectRowNum = marketBrandMapper.insertSelective(marketBrand);
        session.commit();
        session.close();
        return effectRowNum;

    }

    @Override
    public int update(MarketBrand marketBrand) {
        SqlSession session = MybatisUtils.getSession();
        MarketBrandMapper mapper = session.getMapper(MarketBrandMapper.class);
        int effectRowNum = mapper.updateByPrimaryKeySelective(marketBrand);
        session.commit();
        session.close();
        return effectRowNum;

    }

    @Override
    public int delete(MarketBrand marketBrand) {
        SqlSession session = MybatisUtils.getSession();
        int effectRowNum =session.getMapper(MarketBrandMapper.class).deleteByPrimaryKey(marketBrand.getId());

        session.commit();
        session.close();
        return effectRowNum;
    }


}
